#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <std_msgs/String.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
#include <tcr100/VehicleEncoder.h>
#include "math.h"
//#define DBL_MAX 1.18973149535723176502e+4932L
double x = 0.0;
double y = 0.0;
double th = 0.0;
//double vx = 0.0;
//double vy = 0.0;
//double vth = 0.0;

double vx_odom = 0.0;
double vy_odom = 0.0;
double vth_odom = 0.0;

void callback(const tcr100::VehicleEncoder& msg)
{    
    x=msg.pos_x;
    y=msg.pos_y;
    th=msg.pos_rotate;

    vx_odom=msg.vel_x;
    vy_odom=msg.vel_y;
    vth_odom=msg.vel_rotate;
}
int main(int argc, char** argv)
{
    ros::init(argc, argv, "odometry_publish");
    ros::NodeHandle n;
    ros::Time current_time;
    ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odometry_encoder", 50);
    ros::Subscriber sub= n.subscribe("/odom_encoder",1000,callback);
    ros::Rate r(25.0);
    tf::TransformBroadcaster odom_broadcaster;



    while(n.ok())
    {

        ros::spinOnce();               // check for incoming messages
        current_time = ros::Time::now();
        //compute odometry in a typical way given the velocities of the robot
        geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
        //first, we'll publish the transform over tf
        geometry_msgs::TransformStamped odom_trans;
        geometry_msgs::PoseWithCovariance odom_covariance;
        odom_trans.header.stamp = current_time;
        odom_trans.header.frame_id = "odom";
        odom_trans.child_frame_id = "base_link";

        odom_trans.transform.translation.x = x;
        odom_trans.transform.translation.y = y;
        odom_trans.transform.translation.z = 0.0;
        odom_trans.transform.rotation = odom_quat;
        odom_broadcaster.sendTransform(odom_trans);
        //next, we'll publish the odometry message over ROS
        nav_msgs::Odometry odom;
        odom.header.stamp = current_time;
        odom.header.frame_id = "odom";
        //set the position
        odom.pose.pose.position.x = x;
        odom.pose.pose.position.y = y;
        odom.pose.pose.position.z = 0.0;
        odom.pose.pose.orientation = odom_quat;
//        odom.pose.covariance[0]=0.1;//{1e-3,0,0,0,0,0,0,1e-3,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e3};
//        odom.pose.covariance[7]=0.1;
//        odom.pose.covariance[35]=0.05;
//        odom.pose.covariance[14]=DBL_MAX;
//        odom.pose.covariance[21]=DBL_MAX;
//        odom.pose.covariance[28]=DBL_MAX;
        odom.pose.covariance={1e-3,0,0,0,0,0,0,1e-3,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e3};

//        //set the velocity
        odom.child_frame_id = "base_link";
        odom.twist.twist.linear.x = vx_odom;
        odom.twist.twist.linear.y = vy_odom;
        odom.twist.twist.angular.z = vth_odom;
        odom.twist.covariance={1e-3,0,0,0,0,0,0,1e-3,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e3};

        //odom.twist.covariance={1e-3,0,0,0,0,0,0,1e-3,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e6,0,0,0,0,0,0,1e3};
        //publish the message
        odom_pub.publish(odom);
        r.sleep();
    }
}
